package com.honestpeak.datafusion.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.honestpeak.core.datasource.DataSourceContextHolder;
import com.honestpeak.core.datasource.DataSourceType;
import com.honestpeak.datafusion.mapper.ClubMemberInfoMapper;
import com.honestpeak.datafusion.mapper.StaminaResultMapper;
import com.honestpeak.datafusion.model.ClubMemberInfo;
import com.honestpeak.datafusion.model.StaminaResult;
import com.honestpeak.datafusion.model.StuProjectMark;
import com.honestpeak.datafusion.service.StaminaService;
@Service
public class StaminaServiceImpl implements StaminaService{
	@Autowired
    private ClubMemberInfoMapper clubMemberInfoMapper;
	@Autowired
    private StaminaResultMapper staminaResultMapper;


	@Override
	public List<StaminaResult> getClubHealthByYearAndproject(Long yearTermId, Long projectId) {
		Map<String, List<ClubMemberInfo>> itemsMap = Maps.newHashMap();
		List<StaminaResult> list = new ArrayList<StaminaResult>();
		StaminaResult sResult = null;
		DataSourceContextHolder.setDbType(DataSourceType.SPORTS);
		Long year = clubMemberInfoMapper.getYearById(yearTermId);
		List<ClubMemberInfo> cmList = clubMemberInfoMapper.getAllClubMember(yearTermId);
		cmList.stream().forEach(x -> System.out.println(x.toString()));
		itemsMap = cmList.stream().collect(Collectors.groupingBy(ClubMemberInfo::getClubName));
		List<String> studentNoList =  null;
		DataSourceContextHolder.setDbType(DataSourceType.STAMINA);
		for(String key : itemsMap.keySet()){
			sResult = new StaminaResult();    			
			studentNoList = new ArrayList<String>();
			List<ClubMemberInfo> value = itemsMap.get(key);
			for(int i=0; i < value.size(); i++){
				studentNoList.add(value.get(i).getStudentNo());    				
			}
			sResult = staminaResultMapper.getClubLevelNum(studentNoList,projectId,year);
			if(sResult != null){
				sResult.setName(key);
    			list.add(sResult);
			}else{
				list.add(new StaminaResult(key,0,0,0,0));
			}
        }
		DataSourceContextHolder.clearDbType();
		return list;
	}


	@Override
	public List<StaminaResult> getClubHealthByYearAndClubId(Long yearTermId, Long clubId) {
		Map<String, List<ClubMemberInfo>> itemsMap = Maps.newHashMap();
		List<StaminaResult> list = new ArrayList<StaminaResult>();
		StaminaResult sResult = null;
		StringBuilder str = new StringBuilder("");
    	String str1="";
		DataSourceContextHolder.setDbType(DataSourceType.SPORTS);
		Long year = clubMemberInfoMapper.getYearById(yearTermId);
		List<ClubMemberInfo> cmList = clubMemberInfoMapper.getMemberByClubIdAndYTId(yearTermId,clubId);
		
		//TreeMap默认为按照key升序,不指定第二个参数默认HashMap(降序)
		itemsMap = cmList.stream().collect(Collectors.groupingBy(ClubMemberInfo::getClubName,TreeMap::new,Collectors.toList()));
		List<String> studentNoList =  null;
		DataSourceContextHolder.setDbType(DataSourceType.STAMINA);
		for(String key : itemsMap.keySet()){
			sResult = new StaminaResult();    			
			studentNoList = new ArrayList<String>();
			List<ClubMemberInfo> value = itemsMap.get(key);
			for(int i=0; i < value.size(); i++){
				studentNoList.add(value.get(i).getStudentNo());    				
			}
			sResult = staminaResultMapper.getClubLevelNum(studentNoList,null,year);
			if(sResult != null){
				sResult.setName(key);
    			list.add(sResult);
			}else {
				list.add(new StaminaResult(key,0,0,0,0));
			}
		}
		
    	if(list.size()<3){		//当返回的数据不是初中高3个等级时，添加0数据
			if(list.size()==0){
				list.add(new StaminaResult("初级会员",0,0,0,0));
				list.add(new StaminaResult("中级会员",0,0,0,0));
				list.add(new StaminaResult("高级会员",0,0,0,0));
			}else if(list.size()==1){
				str1 = list.get(0).getName();
				if(StringUtils.equals(str1, "初级会员")){
					list.add(new StaminaResult("中级会员",0,0,0,0));
					list.add(new StaminaResult("高级会员",0,0,0,0));
				}else if(StringUtils.equals(str1, "中级会员")){
					list.add(0, new StaminaResult("初级会员",0,0,0,0));
					list.add(new StaminaResult("高级会员",0,0,0,0));
				}else if(StringUtils.equals(str1, "高级会员")){
					list.add(0, new StaminaResult("初级会员",0,0,0,0));
					list.add(1, new StaminaResult("中级会员",0,0,0,0));
				}
			}else if(list.size()==2){
				list.stream().forEach(x -> {
		    		str.append(x.getName());
				});
				if(!StringUtils.equals(str, "初级会员")){
					list.add(0,new StaminaResult("初级会员",0,0,0,0));
				}else if(!StringUtils.equals(str, "中级会员")){
					list.add(1,new StaminaResult("中级会员",0,0,0,0));
				}else if(!StringUtils.equals(str, "高级会员")){
					list.add(new StaminaResult("高级会员",0,0,0,0));
				}
			}
		}		
		DataSourceContextHolder.clearDbType();
		return list;
	}
		
	@Override
	public List<StaminaResult> getLevelNumByYear(Long year, String[] departNames) {		
		Map<String, List<ClubMemberInfo>> itemsMap = Maps.newHashMap(); 
		List<StaminaResult> list = new ArrayList<StaminaResult>();
		StaminaResult sResult = null;
    	DataSourceContextHolder.setDbType(DataSourceType.SPORTS);
    	List<ClubMemberInfo> departStudentList = clubMemberInfoMapper.getAllStudent(departNames);
    	itemsMap = departStudentList.stream().collect(Collectors.groupingBy(ClubMemberInfo::getClubName));
		List<String> studentNoList =  null;
		DataSourceContextHolder.setDbType(DataSourceType.STAMINA);
		for(String key : itemsMap.keySet()){
			sResult = new StaminaResult();    			
			studentNoList = new ArrayList<String>();
			List<ClubMemberInfo> value = itemsMap.get(key);
			for(int i=0; i < value.size(); i++){
				studentNoList.add(value.get(i).getStudentNo());    				
			}
			sResult = staminaResultMapper.getClubLevelNum(studentNoList,null,year);
			if(sResult != null){
				sResult.setName(key);
    			list.add(sResult);
			}else{
				list.add(new StaminaResult(key,0,0,0,0));
			}
        }
    	DataSourceContextHolder.clearDbType();
    	return list;
	}


	@Override
	public Map<String, Object> getMarkList(Long year, String studentNo) {
		DataSourceContextHolder.setDbType(DataSourceType.STAMINA);
    	List<StuProjectMark> stuMarkList = clubMemberInfoMapper.getMarkListByYearAndStuNo(year, studentNo);
		Map<String, Object> resultMap = Maps.newHashMap();
    	List<Map<String, Object>> itemsList = Lists.newArrayList();
    	if(stuMarkList.isEmpty()){
    		resultMap.put("status",-1);		//根据学号和学年没查到成绩信息时，状态为-1
    	}else{
    		resultMap.put("status",1);
	    	resultMap.put("studentNo", stuMarkList.get(0).getStudentNo());
	    	resultMap.put("stuName", stuMarkList.get(0).getStuName());
	    	resultMap.put("sex", stuMarkList.get(0).getSex());
	    	StringBuilder height = new StringBuilder("");//new StringBuilder();//身高/体重
	    	stuMarkList.stream().forEach(x -> {
	    		if(StringUtils.equals(x.getProjectName(), "身高")){
	    			height.append(x.getMark());
	    		}else if(StringUtils.equals(x.getProjectName(), "体重")){
	    			height.append("/").append(x.getMark()) ;
	    		}else{
		    		Map<String, Object> map = Maps.newHashMap();
		    		map.put("projectName", x.getProjectName());
		    		if(StringUtils.equals(x.getProjectName(), "身高/体重")){
		    			map.put("mark", height.toString());
		    		}else{
		    			map.put("mark", x.getMark());
		    		}
		    		map.put("level", x.getLevel());
		    		itemsList.add(map);
	    		}
	    	});
	    	resultMap.put("items",itemsList);
    	}
    	DataSourceContextHolder.clearDbType();
    	return resultMap;
	}


	@Override
	public List<StaminaResult> getProjectLevelNum(Long yearTermId, Long departId, Long classId) {
		DataSourceContextHolder.setDbType(DataSourceType.SPORTS);
		Long year = clubMemberInfoMapper.getYearById(yearTermId);
    	List<String> stuList = clubMemberInfoMapper.getStuListByDepartIdAndclassId(departId,classId);
    	DataSourceContextHolder.setDbType(DataSourceType.STAMINA);
    	List<StaminaResult> projectLevelNumList = staminaResultMapper.getProjectLevelNum(stuList,year);
    	DataSourceContextHolder.clearDbType();
		return projectLevelNumList;
	}
}


	
